<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="操作的类型" prop="operateType">
      <el-input v-model="dataForm.operateType" placeholder="操作的类型"></el-input>
    </el-form-item>
    <el-form-item label="砍价金额" prop="kanyue">
      <el-input v-model="dataForm.kanyue" placeholder="砍价金额"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="剩余余额" prop="moneyYu">
      <el-input v-model="dataForm.moneyYu" placeholder="剩余余额"></el-input>
    </el-form-item>
    <el-form-item label="商品 id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品 id"></el-input>
    </el-form-item>
    <el-form-item label="砍价id" prop="barginid">
      <el-input v-model="dataForm.barginid" placeholder="砍价id"></el-input>
    </el-form-item>
    <el-form-item label="商品姓名" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品姓名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          operateType: '',
          kanyue: '',
          username: '',
          moneyYu: '',
          goodsId: '',
          barginid: '',
          goodsName: '',
          createdAt: '',
          updatedAt: ''
        },
        dataRule: {
          operateType: [
            { required: true, message: '操作的类型不能为空', trigger: 'blur' }
          ],
          kanyue: [
            { required: true, message: '砍价金额不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          moneyYu: [
            { required: true, message: '剩余余额不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品 id不能为空', trigger: 'blur' }
          ],
          barginid: [
            { required: true, message: '砍价id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品姓名不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/aoptest/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.operateType = data.aopTest.operateType
                this.dataForm.kanyue = data.aopTest.kanyue
                this.dataForm.username = data.aopTest.username
                this.dataForm.moneyYu = data.aopTest.moneyYu
                this.dataForm.goodsId = data.aopTest.goodsId
                this.dataForm.barginid = data.aopTest.barginid
                this.dataForm.goodsName = data.aopTest.goodsName
                this.dataForm.createdAt = data.aopTest.createdAt
                this.dataForm.updatedAt = data.aopTest.updatedAt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/aoptest/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'operateType': this.dataForm.operateType,
                'kanyue': this.dataForm.kanyue,
                'username': this.dataForm.username,
                'moneyYu': this.dataForm.moneyYu,
                'goodsId': this.dataForm.goodsId,
                'barginid': this.dataForm.barginid,
                'goodsName': this.dataForm.goodsName,
                'createdAt': this.dataForm.createdAt,
                'updatedAt': this.dataForm.updatedAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
